//
//  SLKeychain.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/5.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import Foundation
import KeychainAccess
import AdSupport
import AppTrackingTransparency

struct SLKeychain {

    static let KEYCHAIN_SERVICE:String = "com.coolBoy.sulitLoan"  // 需要项目唯一性，建议使用项目的 bundleId
    static let UUID_KEY:String = "KEY_UUID"
    static let IDFA_KEY:String = "KEY_IDFA"
    static let IDFV_KEY:String = "KEY_IDFV"
    
    static func getUUID() -> String{
        let keychain = Keychain(service: KEYCHAIN_SERVICE)
        var uuid:String = ""
        do {
            uuid = try keychain.get(UUID_KEY) ?? ""
        }
        catch let error {
            print(error)
        }
        if uuid.isEmpty {
            uuid = UUID().uuidString
            do {
                try keychain.set(uuid, key: UUID_KEY)
            }
            catch let error {
                print(error)
                uuid = ""
            }
        }
        return uuid
    }
    static func getIDFA()-> String{
        var idfa:String = ""
        if idfa.isEmpty || idfa == "00000000-0000-0000-0000-000000000000" {
            if #available(iOS 14, *) {
               ATTrackingManager.requestTrackingAuthorization { (status) in
                   switch status {
                    case .denied:
                       debugPrint("用户拒绝")
                       break
                   case .authorized:
                        debugPrint("用户允许")
                       debugPrint("IDFA:\(ASIdentifierManager.shared().advertisingIdentifier.uuidString)")
                       // 用户同意访问
                       let idfastring = ASIdentifierManager.shared().advertisingIdentifier.uuidString
                       SLKeychain.setIDFA(idfastring)
                       idfa = idfastring
                       break
                   case .notDetermined:
                       debugPrint("用户没有选择")
                   default:
                       break
                   }
               }
           } else {
               // iOS13及之前版本，继续用以前的方式
               if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
                   let idfastring = ASIdentifierManager.shared().advertisingIdentifier.uuidString
                   SLKeychain.setIDFA(idfastring)
                   idfa = idfastring
               } else {
                   debugPrint("用户未打开IDFA开关")
               }
           }
        }
        
        return idfa
    }
    static func setIDFA(_ value: String){
        let keychain = Keychain(service: KEYCHAIN_SERVICE)
        do {
            try keychain.set(value, key: IDFA_KEY)
            print("存储成功：\(value)")
        }catch let error {
            print("存储失败：\(error)")
     
        }
    }
    static func getIDFV() -> String{
        var idfv = ""
        let keychain = Keychain(service: KEYCHAIN_SERVICE)
        do {
            idfv = try keychain.get(IDFV_KEY) ?? ""
            print("获取成功：\(idfv)")
        }
        catch let error {
            print(error)
        }
        if idfv.isEmpty || idfv.count < 5{
            if let idfvString = UIDevice.current.identifierForVendor?.uuidString{
                idfv = idfvString
            }
            do {
                try keychain.set(idfv, key: IDFV_KEY)
                print("存储成功：\(idfv)")
            }catch let error {
                print("存储失败：\(error)")
            }
        }
        return idfv
    }

    
}
